package ArrayList;

import java.util.ArrayList;
//import java.util.Collections;
import java.util.Random;

//import static java.util.Collections.shuffle;

//表示每个玩家的手牌
class HandCard {
    private ArrayList<Card> cards;
}


class Card {
    private String suit;         //花色
    private String rank;         //点数

    public Card(String suit,String rank) {
        this.suit = suit;
        this.rank = rank;
    }
    public String getSuit() {
        return suit;
    }
    public String getRank() {
        return rank;
    }
    public void setSuit(String suit) {
        this.suit = suit;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }


    public String toString() {
        return this.suit + " " + this.rank;
    }
}

public class Demo1 {
    public static void main(String[] args) {
    ArrayList<Card> deck = createDeck();
        System.out.println("原始牌组：" + deck);

        //洗牌
        shuffle(deck);
        System.out.println("洗牌后的：" +deck);

        //发牌，假设有三个人，每个人发 5 张牌      3行5列的二维数组
        ArrayList<ArrayList<Card>> hands = new ArrayList<ArrayList<Card>>();
        //ArrayList<HandCard> handCards = new ArrayList<HandCard>();

        //也是一个ArrayList，里面的每个元素都是一个ArrayList，当前这个ArrayList的长度为0
        //往里面添加三个元素，表示三个玩家的手牌
        for (int i = 0; i < 3; i++) {
            ArrayList<Card> hand = new ArrayList<Card>();
            hands.add(hand);
        }

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                ArrayList<Card> currentHand = hands.get(j);
                Card card = deck.remove(0);
                currentHand.add(card);
            }
        }

        //打印每个人的手牌
        for (int i = 0; i < 3; i++) {
            System.out.println("第" + i + "个人的手牌：" + hands.get(i));
        }
    }

    //自己实现洗牌
    public static void shuffle(ArrayList<Card> deck) {
        //把整个 ArrayList 从后往前遍历
        //针对每次取到一张牌，就生成一个随机下标，拿着当前位置的牌和随机下标位置的牌，进行交换
        Random random = new Random();
        for (int i = deck.size()-1; i > 0 ; i--) {
            int j = random.nextInt(deck.size());
            //交换两张牌的位置
            Card tmp = new Card(deck.get(i).getSuit(),deck.get(i).getRank());
            deck.set(i,deck.get(j));
            deck.set(j,tmp);
        }
    }

    public static ArrayList<Card> createDeck() {
        //创建一副扑克牌
        ArrayList<Card> deck = new ArrayList<Card>();
        String[] suits = {"♠","♦","❤","♣"};
        for (String suit : suits) {
            //在每个花色中，生成对应点数的牌
            for (int i = 2; i < 10; i++) {
                Card card = new Card(suit,"" + i);
                deck.add(card);
            }
            Card cardj = new Card(suit,"j");
            Card cardQ = new Card(suit,"Q");
            Card cardK = new Card(suit,"K");
            Card cardA = new Card(suit,"A");
            deck.add(cardj);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
        return deck;
    }
}
